package com.example.pro1.controller.zhao;


import com.example.pro1.entity.zhao.OrderInfo;
import com.example.pro1.entity.zhao.RefundInfo;
import com.example.pro1.enums.OrderStatus;
import com.example.pro1.service.zhao.table.OrderInfoService;
import com.example.pro1.service.zhao.table.RefundInfoService;
import com.example.pro1.valu.R;
import com.example.pro1.valu.Standard;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin//开放前端跨域访问
@Api(tags = "订单管理")
@RestController
@RequestMapping("/api/order-info")

public class OrderInfoController {

    @Resource
    private OrderInfoService orderInfoService;
    @Resource
    private RefundInfoService refundInfoService;

    @GetMapping("/find/byid/or")
    public Standard findbyid() {

        return null;
    }

    // @GetMapping("/find/all")
    @GetMapping("/list")
    public Standard findall() {
        List<OrderInfo> list = orderInfoService.findAllByCreatTimeDesc();

        return R.ok().data("list", list);
    }

    /**
     * 查询本地订单状态
     */
    @ApiOperation("查询本地订单状态")
    @GetMapping("/query-order-status/{orderNo}")
    public Standard queryOrderStatus(@PathVariable String orderNo) {
        String orderStatus = orderInfoService.getOrderInfoStatus(orderNo);
        if (OrderStatus.SUCCESS.getType().equals(orderStatus)) {
            //支付成功
            return R.ok().setMessage(orderStatus);
        }
        return R.ok().setCode(101).setMessage(orderStatus);

    }
    //未支付
    @GetMapping("/query-order-status/nopay")
    public Standard queryNopay()
    {
        return R.ok().setCode(101).data("未支付",orderInfoService.getOrdernopayStatus());
    }

    //请求退款
    @GetMapping("/query-order-status/refund/{oderNum}/{reason}")
    public Standard refund(@PathVariable String oderNum,@PathVariable String reason)
    {

        orderInfoService.updateOrderInfo(oderNum,OrderStatus.PLE);
        refundInfoService.createRefundByOrderNo(oderNum,reason);
        return R.ok().setCode(101).data("status",orderInfoService.getOrderInfoStatus(oderNum));
    }
    //确认收货
    @GetMapping("/query-order-status/get/{oderNum}")
    public Standard haveget(@PathVariable String oderNum)
    {

        orderInfoService.updateOrderInfo(oderNum,OrderStatus.HAVEGET);

        return R.ok().setCode(101).data("status",orderInfoService.getOrderInfoStatus(oderNum));
    }


}
